<%--
 点击角色查询角色信息和门户信息
  User: hc
  Date: 14-5-15
  Time: 下午16:10
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <%@include file="../../common/include_list_head.jsp" %>
    <title></title>


    <script type="text/javascript">

        $(document).ready(function () {

            //菜单按层次向后两个空格
            $("td[id^='td_']").each(function (i) {
                var id_len_3 = $(this).attr("id").length / 3 - 2;
                var str = "";
                if (id_len_3 > 0) {
                    for (var i = 0; i < id_len_3; i++) {
                        str += "&nbsp;&nbsp;&nbsp;";
                    }
                }
                $(this).html(str + $(this).html());
            });
            var roleMenusBuffer = document.getElementById("roleMenusBuffer").value;

            roleMenusBuffer = roleMenusBuffer.split(",");
            for (var i = 0; i < roleMenusBuffer.length; i++) {
                $("#menu_" + roleMenusBuffer[i]).attr("checked", "checked");
                tochangeMenu(roleMenusBuffer[i]);
            }


            //查看状态保存按钮隐藏 输入框只读等
            doReadOnly();
        });

        //更新菜单信息
        function tochangeMenu(obj) {
            if (obj.checked) { //菜单被选中时

                //菜单选中时其上级菜单默认选中
                var len_3 = obj.value.length / 3;
                for (var i = 2; i < len_3; i++) {
                    var str = obj.value.substr(0, i * 3);
                    if (!$("#menu_" + str).attr("checked")) { //上级菜单未选中时
                        $("#menu_" + str).attr("checked", "checked");
                        var menues = document.getElementById("menus").value;
                        if (menues == null || menues == '') {
                            document.getElementById("menus").value = $("#menu_" + str).val();

                        } else {
                            document.getElementById("menus").value += "," + $("#menu_" + str).val();

                        }
                    }
                }
                //菜单选中时其下级菜单默认选中
                $("input[id^='menu_" + obj.value + "']").each(function (i) {
                    if (!this.checked) { //上级菜单未选中时
                        $(this).attr("checked", "checked");
                        var menues = document.getElementById("menus").value;
                        if (menues == null || menues == '') {
                            document.getElementById("menus").value = $(this).val();
                        } else {
                            document.getElementById("menus").value += "," + $(this).val();

                        }
                    }
                });
                var menues = document.getElementById("menus").value;
                if (menues == null || menues == '') {
                    document.getElementById("menus").value = obj.value;

                } else {
                    document.getElementById("menus").value += "," + obj.value;
                }
            } else { //菜单取消选中时
                //下级菜单取消选中
                $("input[id^='menu_" + obj.value + "']").attr("checked", "");
                //更新菜单信息
                var menues = document.getElementById("menus").value;
                var menue = menues.split(",");
                var curr_menues = "";
                for (var i = 0; i < menue.length; i++) {
                    if (menue[i].indexOf(obj.value) != 0) {
                        curr_menues += menue[i] + ",";
                    }
                }
                if (curr_menues.lastIndexOf(",") == curr_menues.length - 1) {
                    curr_menues = curr_menues.substr(0, curr_menues.length - 1);
                }

                document.getElementById("menus").value = curr_menues;
            }
        }

        function changeAll(obj) {
            document.getElementById("menus").value=null;
            if (!obj.checked) {
                $("input[name^='selectNodeMenu']").each(function (i) { //二级菜单取消选中时,下级菜单全部取消选中

                    if (this.checked) { //该菜单的按钮已选中
                        $(this).attr("checked", ""); //选中菜单下的按钮后默认选中该菜单
                    }
                });
                //更新菜单信息
                var menues = document.getElementById("menus").value;

                var menue = menues.split(",");
                var curr_menues = "";
                for (var i = 0; i < menue.length; i++) {
                    if (menue[i].indexOf(obj.value) != 0) {
                        curr_menues += menue[i] + ",";
                    }
                }

                if (curr_menues.lastIndexOf(",") == curr_menues.length - 1) {
                    curr_menues = curr_menues.substr(0, curr_menues.length - 1);
                }
                document.getElementById("menus").value = null;
//                window.parent.document.getElementById("menues").value = curr_menues;
            } else { //选中二级菜单时,默认其下级菜单全部选中
                $("input[name^='selectNodeMenu']").each(function (i) { //二级菜单取消选中时,下级菜单全部取消选中
                    $(this).attr("checked", "checked"); //选中菜单下的按钮后默认选中该菜单
                    var menues = document.getElementById("menus").value;
                    if (menues == null || menues == '') {
                        document.getElementById("menus").value = this.value;
                    } else {
                        document.getElementById("menus").value += "," + this.value;
                    }
                });
            }
        }
    </script>
    <script type="text/javascript">
        function dataCheck() {
            var menus = document.getElementById("menus").value;
            var roleid = document.getElementById("roleid").value
            if(menus==null||menus==''){
                if(window.confirm('确定要清空该角色门户资源吗？')){
                    var url = "./portalAuthorization-saveRoleMenu.action?menus="+ menus +"&roleid="+roleid;
                    document.forms[0].action= url
                    document.forms[0].submit();
                    return true;
                }else{

                    return false;
                }
            }
            var url = "./portalAuthorization-saveRoleMenu.action?menus="+ menus +"&roleid="+roleid;
            document.forms[0].action= url
            document.forms[0].submit();
        }
        function clean(){
            $("input[name^='selectNodeMenu']").each(function (i) { //二级菜单取消选中时,下级菜单全部取消选中

                if (this.checked) { //该菜单的按钮已选中
                    $(this).attr("checked", ""); //选中菜单下的按钮后默认选中该菜单
                }
            });
            document.getElementById("menus").value=null;
        }
    </script>
</head>
<body>
<div id="scrollContent">
    <s:if test="actionErrors.size()>0 || actionMessages.size()>0 || fieldErrors.size()>0">
        <div id="SystemErrorMessage">
            <s:actionerror/>
            <s:actionmessage/>
            <s:fielderror/>
        </div>
    </s:if>
    <s:form name="theform" method="post" theme="simple">
        <s:hidden name="menus" id="menus" value="%{roleMenusBuffer}"/>
        <s:hidden name="roleMenusBuffer" id="roleMenusBuffer" value="%{roleMenusBuffer}"/>
        <s:hidden name="roleid" id="roleid" value="%{roleid}"/>
        <div class="box1" panelWidth="100%">
            <table class="tableStyle" transMode="true" footer="normal">
                <tr>
                    <td width="10%" align="right">角色名称：</td>
                    <td width="20%" align="left" nowrap="nowrap"><s:textfield id="rolename" name="tsysRole.rolename"
                                                                              cssStyle="width: 150px"
                                                                              maxlength="15"
                                                                              cssClass="validate[required,length[1,15]]"
                                                                              readonly="true"/>
                    </td>
                    <td width="13%" align="right">状态：</td>
                    <td width="20%" align="left"><s:select name="tsysRole.state" list="#{'1':'启用','0':'禁用'}"
                                                           listKey="key" disabled="true"
                                                           listValue="value"/></td>
                    <td width="37%">&nbsp;</td>
                </tr>
                <tr>
                    <td align="right" nowrap="nowrap">描述：</td>
                    <td colspan="4" align="left">
		      	<span class="float_left">
					<s:textarea name="tsysRole.memo" id="memo" cssStyle="width:400px;" readonly="true" disabled="true"
                                cssClass="validate[length[0,50]]"></s:textarea>
				</span></td>
                </tr>
            </table>
            <div style="margin-top:20px">
                <table class="tableStyle" headFixMode="true" id="tab" style="font-size: 12px">
                    <tr>
                        <td align="left" colspan="100">
                            <s:checkbox id="menu_%{roleid}" name="selectNodeMenu"
                                        fieldValue="%{roleid}"
                                        onclick="changeAll(this)"/>
                            <label for="menu_<s:property value="upmenuid"/>"><s:property value="menuname"/></label>
                            <s:hidden name="upmenuid"/><s:hidden name="roleid"/>

                        </td>
                    </tr>
                    <tr>

                        <th align="center" width="20%">资源名称</th>
                        <th align="center" width="15%">所属系统</th>
                        <th align="center" width="15%">资源状态</th>
                        <th align="center" width="15%">资源类型</th>
                        <th align="center" width="15%">编号</th>
                        <th align="center" width="30%">资源路径</th>

                    </tr>

                    <s:iterator value="menulist" id="lm" status="i">
                        <tr>

                            <td width="180" id="td_<s:property value='menuid'/>">

                                <s:checkbox id="menu_%{menuid}" name="selectNodeMenu" fieldValue="%{menuid}"
                                            onclick="tochangeMenu(this)"/>
                                <label for="menu_<s:property value="%{menuid}" />"><s:property value="menuname"/>
                                    <span id="span_<s:property value='%{#i.index}'/>"></span></label>
                            </td>

                            <td align="center">
                                <s:property value="portalname"/>
                            </td>
                            <td align="center">
                                <s:if test="state==0">禁用</s:if>
                                <s:else>可用</s:else>
                            </td>
                            <td align="center">
                                <s:property value="srctype"/></td>
                            <td align="center">
                                <s:property value="bizcode"/></td>
                            <td align="center">
                                <s:property value="menupath"/>
                            </td>
                        </tr>
                    </s:iterator>
                </table>
            </div>
        </div>
        <div class="padding_top10">
            <table class="tableStyle" transMode="true">
                <tr>
                    <td colspan="4">
                        <input type="button" id="saveButton" value=" 保存 " onclick="dataCheck()"/>
                        <input type="button" id="backButton" value=" 清除 " onclick="clean()"/>
                        <span style="color: #009"><s:property value="msg"/> </span>
                    </td>
                </tr>
            </table>
        </div>
    </s:form>

</div>

</body>
</html>